<template>
  <el-dialog
    :title="!dataForm.concernId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userPhone">
      <el-input v-model="dataForm.userPhone" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="租房id" prop="rentId">
      <el-input v-model="dataForm.rentId" placeholder="租房id"></el-input>
    </el-form-item>
    <el-form-item label="新房/二手房id" prop="roomId">
      <el-input v-model="dataForm.roomId" placeholder="新房/二手房id"></el-input>
    </el-form-item>
    <el-form-item label="房屋类型" prop="houseType">
      <el-input v-model="dataForm.houseType" placeholder="房屋类型"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          concernId: 0,
          userPhone: '',
          rentId: '',
          roomId: '',
          houseType: ''
        },
        dataRule: {
          userPhone: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          rentId: [
            { required: true, message: '租房id不能为空', trigger: 'blur' }
          ],
          roomId: [
            { required: true, message: '新房/二手房id不能为空', trigger: 'blur' }
          ],
          houseType: [
            { required: true, message: '房屋类型不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.concernId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.concernId) {
            this.$http({
              url: this.$http.adornUrl(`/user/houseconcern/info/${this.dataForm.concernId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userPhone = data.houseConcern.userPhone
                this.dataForm.rentId = data.houseConcern.rentId
                this.dataForm.roomId = data.houseConcern.roomId
                this.dataForm.houseType = data.houseConcern.houseType
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/houseconcern/${!this.dataForm.concernId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'concernId': this.dataForm.concernId || undefined,
                'userPhone': this.dataForm.userPhone,
                'rentId': this.dataForm.rentId,
                'roomId': this.dataForm.roomId,
                'houseType': this.dataForm.houseType
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
